package com.ruoyi.zxjd.document.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zxjd.document.domain.DocumentArrange;
import com.ruoyi.zxjd.document.domain.DocumentFile;
import com.ruoyi.zxjd.document.domain.DocumentProgress;
import com.ruoyi.zxjd.document.service.IDocumentArrangeService;
import com.ruoyi.zxjd.document.service.IDocumentFileService;
import com.ruoyi.zxjd.document.service.IDocumentProgressService;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公文处理Controller
 * 
 * @author lcc
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/document/progress")
public class DocumentProgressController extends BaseController
{
    private String prefix = "document/progress";

    @Autowired
    private IDocumentProgressService documentProgressService;
    @Autowired
    private IDocumentArrangeService documentArrangeService;

    @Autowired
    private IDocumentFileService documentFileService;

    @RequiresPermissions("document:progress:view")
    @GetMapping()
    public String progress()
    {
        return prefix + "/progress";
    }

    /**
     * 查询公文处理列表
     */
    @RequiresPermissions("document:progress:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DocumentProgress documentProgress)
    {
        startPage();
        List<DocumentProgress> list = documentProgressService.selectDocumentProgressList(documentProgress);
        return getDataTable(list);
    }

    /**
     * 导出公文处理列表
     */
    @RequiresPermissions("document:progress:export")
    @Log(title = "公文处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DocumentProgress documentProgress)
    {
        List<DocumentProgress> list = documentProgressService.selectDocumentProgressList(documentProgress);
        ExcelUtil<DocumentProgress> util = new ExcelUtil<DocumentProgress>(DocumentProgress.class);
        return util.exportExcel(list, "progress");
    }

    /**
     * 新增公文处理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存公文处理
     */
    @RequiresPermissions("document:progress:add")
    @Log(title = "公文处理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DocumentProgress documentProgress)
    {
        return toAjax(documentProgressService.insertDocumentProgress(documentProgress));
    }

    /**
     * 修改公文处理
     */
    @GetMapping("/edit/{progressId}")
    public String edit(@PathVariable("progressId") Long progressId, ModelMap mmap)
    {
        DocumentProgress documentProgress = documentProgressService.selectDocumentProgressById(progressId);
        mmap.put("documentProgress", documentProgress);
        return prefix + "/edit";
    }

    /**
     * 修改保存公文处理
     */
    @RequiresPermissions("document:progress:edit")
    @Log(title = "公文处理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DocumentProgress documentProgress)
    {
        return toAjax(documentProgressService.updateDocumentProgress(documentProgress));
    }

    /**
     * 删除公文处理
     */
    @RequiresPermissions("document:progress:remove")
    @Log(title = "公文处理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(documentProgressService.deleteDocumentProgressByIds(ids));
    }

    /**
     * 查看公文详情
     */
    @RequiresPermissions("document:progress:detail")
    @GetMapping("/detail/{arrangeId}")
    public String detail(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        DocumentArrange documentArrange = documentArrangeService.selectDocumentArrangeById(arrangeId);
        mmap.put("documentArrange", documentArrange);

        Long[] ids =(Long[]) ConvertUtils.convert(documentArrange.getAttachments().split(","), Long.class);
      List<DocumentFile> documentFileList = documentFileService.selectDocumentFileByIds(ids);
        mmap.put("documentFileList", documentFileList);
        mmap.put("createTime", DateUtils.dateAndTime(documentArrange.getCreateTime()));
        return prefix + "/person/receive_detail";
    }

    /**
     * 接收公文并填写回执信息
     */
    @RequiresPermissions("document:progress:receive")
    @Log(title = "公文处理", businessType = BusinessType.UPDATE)
    @PostMapping("/receive")
    @ResponseBody
    public AjaxResult receive(DocumentProgress documentProgress)
    {
        return toAjax(documentProgressService.receive(documentProgress));
    }

    /**
     * 个人收文
     */
    @GetMapping("/personal")
    public String receiveDocument(ModelMap mmap)
    {
        return prefix + "/person/progress_personal";
    }


    /**
     * 短信提示
     */
    @RequiresPermissions("work:progress:sms")
    @Log(title = "短信提示", businessType = BusinessType.UPDATE)
    @PostMapping( "/smsPrompt")
    @ResponseBody
    public AjaxResult sms(Long progressId)
    {

        return toAjax(documentProgressService.smsPrompt(progressId));
    }




}
